/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.User;
import ejb.UserFacade;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Danilo
 */
@ManagedBean
@SessionScoped
public class HomeManagedBean {

    private User currentUser;
    @EJB
    private UserFacade ufr;

    /** Creates a new instance of HomeManagedBean */
    public HomeManagedBean() {
    }

    public User getCurrentUser() {
        String username = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("id");
        if (username != null) {
            if (currentUser != null) {
                return currentUser;
            }
            try {
                currentUser = ufr.find(username).clone();
            } catch (CloneNotSupportedException ex) {
                Logger.getLogger(HomeManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            currentUser = null;
        }
        return currentUser;
    }
}
